package code.main.controller.hall
{
	import code.app.AppCtrlList;
	import code.app.AppData;
	import code.app.AppDmList;
	import code.app.AppView;
	import code.main.controller.alert.AlertCtrl;
	import code.main.controller.event.EventManager;
	import code.main.controller.event.EventName;
	import code.main.controller.event.MyEvent;
	import code.main.modle.hall.HallData;
	import code.main.modle.lang.Language;
	import code.main.modle.table.Player;
	import code.main.view.hall.HallItemVo;
	import code.main.view.hall.HallView;
	import code.main.view.hall.RoomListItem;
	
	import com.yy.mvc.ctrl.Controller;
	import com.yy.yyui.ButtonGroup;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 *大厅管理器 
	 * @author YY
	 * 
	 */
	public class HallCtrl extends Controller
	{
		private var _hallView:HallView;
		private var _player:Player;
		private var _hallData:HallData;
		public function HallCtrl()
		{
			_player = AppDmList.getInstance().player;
			_hallData = AppDmList.getInstance().hallData;
		}
		
		/**
		 *游戏币变化 
		 * @param e
		 * 
		 */
		private function gameMoneyChangeFunc(e:MyEvent):void {
			var value:int = AppDmList.getInstance().player.gameMoney;
			_hallView.moneyLabel.text = String(value);
		}
		
		override public function start():void {
			_hallView = new HallView();
			addEvent();
			show();
		}
		
		/**
		 *显示大厅 
		 * 
		 */
		public function show():void {
			AppView.getInstance().hallLayer.visible = true;
			AppView.getInstance().hallLayer.addChild(_hallView);
			_hallView.nameTxt.text = _player.userName;
			_hallView.moneyLabel.text = String(_player.gameMoney);
			if(_player.vipLevel != 0) {
				_hallView.vip.visible = true;
				_hallView.vip.gotoAndStop(_player.vipLevel);
			}else {
				_hallView.vip.visible = false;
			}
			refreshUi();
		}
		
		/**
		 *刷新ui 
		 * 
		 */
		private function refreshUi():void {
			AppDmList.getInstance().hallData.reqData(xxFunc);
			function xxFunc():void {
				_hallView.list.items = AppDmList.getInstance().hallData.list;
			}
		}
		
		/**
		 * 关闭大厅
		 */
		public function close():void {
			AppView.getInstance().hallLayer.visible = false;
			if(AppView.getInstance().hallLayer.contains(_hallView)) {
				AppView.getInstance().hallLayer.removeChild(_hallView);
			}
		}
		
		private function addEvent():void {
			_hallView.enterBtn.addEventListener(MouseEvent.CLICK, clickEnterRoomFunc);
			EventManager.getInstance().addEventListener(EventName.GAME_MONEY_CHANGE, gameMoneyChangeFunc);
			_hallView.userListView.showBtnState();
		}
		
		/**
		 *点击进入房间按钮 
		 * @param e
		 * 
		 */
		private function clickEnterRoomFunc(e:Event):void{
			var vo:HallItemVo = _hallView.list.selectedItem;
			if(vo) {
				_player.tableVo.tableId = vo.tableId;
				_player.tableVo.minCarring = vo.minBuyIn;
				_player.tableVo.maxCarring = vo.maxBuyIn;
				AppCtrlList.getInstance().sceneCtrl.enterTable();
			}else {
				AppCtrlList.getInstance().alertCtrl.sysMsgBox(Language.HALL_PROMPT[0]);
			}
		}
		
		private function clickTestBtnFunc(e:Event):void{
			trace("click");
			_player.tableVo.tableId = 1;
			_player.tableVo.minCarring = 20;
			_player.tableVo.maxCarring = 400;
			AppCtrlList.getInstance().sceneCtrl.enterTable();
		}
	}
}